new Vue({
    el: "#container",
    data() {
        return {
            tableSearch: {
                pageIndex: 0,
                limit: 50,
                total: 0,
                dateFormat: "yyyy-MM-dd HH:mm",
                endDate: null,
                startDate: null,
                conName: "",
                site: "",
                handle: null,
            },
            //  发货单
            invoiceSearch: {
                pageIndex: 0,
                limit: 50,
                conName: "",
                site: "",
                invoiceCode: "",
                loading: false,
            },
            //  表数据
            tableDatas: [],
            tableLoading: false,
            //  退料数据弹窗
            invBackDialog: {
                title: "",
                show: false,
                loading: false,
                update: false,
            },
            //  退料数据
            invBackForm: {
                id: null,
                url: "/pro/invoice/back/add",
                render: {
                    invoiceId: null,
                    reType: null,
                    weightStere: null,
                    weightTime: null,
                    remark: null,
                },
            },
            invBackRules: {
                render: {
                    invoiceId: [
                        {required: true, message: '请选择发货单', trigger: 'blur'},
                    ],
                    reType: [
                        {required: true, message: '请选择退料类型', trigger: 'blur'},
                    ],
                    weightStere: [
                        {required: true, message: '请输入过磅退回方量', trigger: 'blur'},
                    ],
                    weightTime: [
                        {required: true, message: '请输入退料时间', trigger: 'blur'},
                    ],
                }
            },
            //  退料类型
            backTypeList: [{
                code: 0,
                desc: "站内退回"
            }, {
                code: 1,
                desc: "站外退回"
            }],
            //  退砼处理弹窗
            operateDialog: {
                show: false,
                loading: false,
            },
            operateForm: {
                render: {
                    id: null,
                    type: null,
                    invoiceId: null,
                    invoiceCode: null,
                    stere: null,
                    remark: null,
                    isAdd: null,
                    sendStere: null,
                },
                //  仅用于展示的数据
                exhibition: {
                    backRemainStere: null,
                },
            },
            //退砼处理,显示发货单弹窗
            backOperateList: [{
                code: 0,
                desc: "作废"
            }, {
                code: 1,
                desc: "转料(本站)"
            }
            ],
            //  退砼处理记录
            recordDialog: {
                show: false,
                loading: false,
            },
            recordList: [],
        }
    },
    // 在模板渲染成html前调用
    created() {
        var that = this;
        that.loadTableDatas();
    },
    // 在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
    mounted() {
        var that = this;
    },
    methods: {
        //  查询表单数据
        searchDatas() {
            let that = this;
            that.tableSearch.pageIndex = 0;
            that.tableSearch.limit = 50;
            that.loadTableDatas();
        },
        // 加载数据
        loadTableDatas: function () {
            let that = this;
            that.tableLoading = true;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/back/page",
                async: true,
                dataType: 'json',
                data: that.tableSearch,
                success: function (res) {
                    if (res.code == 0) {
                        let page = res.data;
                        that.tableDatas = page.records;
                        that.tableSearch.total = page.total;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.tableLoading = false;
                }
            })
        },
        //  显示发货单弹窗
        showAddDialog() {
            let that = this;
            //  加载发货单列表
            that.invBackDialog.show = true;
            that.invBackForm.url = "/pro/invoice/back/add";
            that.invBackDialog.update = false;
            /* Object.keys(that.invBackForm.render).forEach(key => {
                 that.invBackForm.render[key] = null;
             });*/
        },
        //  显示修改弹窗
        showEditDialog(data) {
            let that = this;
            if (data == null) {
                that.$message({
                    showClose: true,
                    message: '请选择修改数据',
                    type: 'error'
                });
                return;
            }
            that.invBackDialog.show = true;
            that.invBackDialog.update = true;
            that.invBackForm = {
                id: data.id,
                url: "/pro/invoice/back/update/" + data.id,
                render: {
                    invoiceId: data.sourceInvoiceId,
                    reType: data.reType + "",
                    weightStere: data.weightStere,
                    weightTime: data.weightTime,
                    remark: data.remark,
                },
            };

        },
        //  选择了发货单
        invoiceChange(inv) {
            let that = this;
            let invId = inv.id;
            if (isNull(invId)) {
                return;
            }
            that.invBackForm.render.invoiceId = invId;
        },
        operateInvChange(inv) {
            let that = this;
            let invId = inv.id;
            if (isNull(invId)) {
                return;
            }
            //  取出作临时操作
            let backRemainStere = that.operateForm.exhibition.backRemainStere;
            that.operateForm.render.invoiceId = invId;
            //  设置方量
            that.operateForm.exhibition.backRemainStere = backRemainStere;
            that.operateForm.render.sendStere = inv.sendStere;
        },
        //  保存弹窗
        saveBakData(formName) {
            let that = this;
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    //  验证成功
                    that.invBackDialog.loading = true;
                    $.ajax({
                        type: "PUT",
                        url: that.invBackForm.url,
                        async: true,
                        dataType: 'json',
                        data: that.invBackForm.render,
                        success: function (res) {
                            if (res.code == 0) {
                                that.$message({
                                    showClose: true,
                                    message: '操作成功',
                                    type: 'success'
                                });
                                that.cancel('invBackForm', 'invBackDialog');
                                that.loadTableDatas();
                            } else {
                                that.$message({
                                    showClose: true,
                                    message: '保存失败：' + res.msg,
                                    type: 'error'
                                });
                            }
                        },
                        error: function (res) {
                            that.$message({
                                showClose: true,
                                message: '请求失败，请联系管理员',
                                type: 'error'
                            });
                            return;
                        },
                        complete: function (XMLHttpRequest, status) {
                            that.invBackDialog.loading = false;
                        }
                    })
                } else {
                    that.$message({
                        showClose: true,
                        message: '验证表单失败.',
                        type: 'error'
                    });
                    return false;
                }
            });
        },
        //  显示操作弹窗
        showOperateDialog(item) {
            let that = this;
            Object.keys(that.operateForm.render).forEach(key => {
                that.operateForm.render[key] = null;
            });
            that.operateForm.render.id = item.id;
            that.operateForm.render.isAdd = item.isAdd == null ? "0" : item.isAdd + "";
            that.operateForm.exhibition.backRemainStere = item.backRemainStere;
            that.operateDialog.show = true;
        },
        saveOperateData: function (formName) {
            let that = this;
            //  验证成功
            let type = that.operateForm.render.type;
            if (type == null) {
                that.$message({
                    showClose: true,
                    message: '请选择操作类型',
                    type: 'error'
                });
                return;
            }
            let stere = that.operateForm.render.stere;
            let backRemainStere = that.operateForm.exhibition.backRemainStere;
            if (stere > backRemainStere) {
                that.$message({
                    showClose: true,
                    message: '操作方量大于了当前待处理方量',
                    type: 'error'
                });
                return;
            }
            if (type != null && type != 0) {
                //  转料操作需要选择发货单
                let invoiceId = that.operateForm.render.invoiceId;
                if (invoiceId == null) {
                    that.$message({
                        showClose: true,
                        message: '转料操作请选择发货单',
                        type: 'error'
                    });
                    return;
                }
            }
            that.operateDialog.loading = true;
            $.ajax({
                type: "PUT",
                url: "/pro/invoice/back/handle/" + that.operateForm.render.id,
                async: true,
                dataType: 'json',
                data: that.operateForm.render,
                success: function (res) {
                    if (res.code == 0) {
                        that.$message({
                            showClose: true,
                            message: '操作成功',
                            type: 'success'
                        });
                        that.loadTableDatas();
                        //  清空数据
                        let handleId = that.operateForm.render.id;
                        let temp = that.operateForm.exhibition.backRemainStere - that.operateForm.render.stere;
                        Object.keys(that.operateForm.render).forEach(key => {
                            that.operateForm.render[key] = null;
                        });
                        Object.keys(that.operateForm.exhibition).forEach(key => {
                            that.operateForm.exhibition[key] = null;
                        });
                        that.operateForm.exhibition.backRemainStere = temp;
                        that.operateForm.render.id = handleId;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '保存失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.operateDialog.loading = false;
                }
            })
        },
        //  显示退砼操作弹窗
        showRecordDialog(item) {
            let that = this;
            //  加载处理记录
            let backId = item.id;
            if (isNull(backId)) {
                that.$message({
                    showClose: true,
                    message: '请选择退砼记录',
                    type: 'error'
                });
                return;
            }
            that.recordDialog.show = true;
            that.loadRecords(backId);
        },
        //  加载退砼操作记录
        loadRecords(backId) {
            let that = this;
            that.recordDialog.loading = true;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/back/record/" + backId,
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        that.recordList = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询操作记录失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.recordDialog.loading = false;
                }
            })
        },
        //  取消添加
        cancel(fromName, dialogName) {
            let that = this;
            Object.keys(that[fromName].render).forEach(key => {
                that[fromName].render[key] = null;
            });
            that[dialogName].show = false;
        },
        //  删除处理记录
        delHandle(backId, handleId) {
            let that = this;
            that.recordDialog.loading = true;
            $.ajax({
                type: "DELETE",
                url: "/pro/invoice/back/del/record/" + handleId,
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        //  删除成功,重新加载操作记录
                        that.loadRecords(backId);
                        that.loadTableDatas();
                    } else {
                        that.$message({
                            showClose: true,
                            message: '删除数据失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.recordDialog.loading = false;
                }
            })
        },
        //  删除退料数据
        deleteBack(backId) {
            let that = this;
            $.ajax({
                type: "DELETE",
                url: "/pro/invoice/back/delete/" + backId,
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        //  删除成功,重新加载操作记录
                        that.loadTableDatas();
                    } else {
                        that.$message({
                            showClose: true,
                            message: '删除数据失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.recordDialog.loading = false;
                }
            })
        },
        // 修改每页数据
        handleSizeChange(val) {
            var that = this;
            that.tableSearch.limit = val;
            // 查询数据
            that.loadTableDatas();
        },
        handleCurrentChange(val) {
            // 设置页数
            var that = this;
            that.tableSearch.pageIndex = val;
            that.loadTableDatas();
        },
    }
})
